package com.example.translateappjava.Utils;

import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import java.util.ArrayList;
import java.util.Iterator;

public class ImageData implements Parcelable {
    private Bitmap image = null;
    private String recognitionResult = "EMPTY";
    private String ImageURL = "NULL";
    private String suggestion = "EMPTY";
    private boolean eatable = true;

    protected ImageData(Parcel in) {
        image = in.readParcelable(Bitmap.class.getClassLoader());
        recognitionResult = in.readString();
        suggestion = in.readString();
        eatable = in.readByte() != 0; // true if byte != 0
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(image, flags);
        dest.writeString(recognitionResult);
        dest.writeString(suggestion);
        dest.writeByte((byte) (eatable ? 1 : 0));
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<ImageData> CREATOR = new Creator<ImageData>() {
        @Override
        public ImageData createFromParcel(Parcel in) {
            return new ImageData(in);
        }

        @Override
        public ImageData[] newArray(int size) {
            return new ImageData[size];
        }
    };

    @Override
    public String toString() {
        return "ImageData{" +
                "image=" + image +
                ", recognitionResult='" + recognitionResult + '\'' +
                ", ImageURL='" + ImageURL + '\'' +
                ", suggestion='" + suggestion + '\'' +
                ", eatable=" + eatable +
                '}';
    }

    public ImageData(Bitmap image, String recognitionResult, String suggestion, boolean eatable) {
        this.image = image;
        this.recognitionResult = recognitionResult;
        this.suggestion = suggestion;
        this.eatable = eatable;
    }

    public Bitmap getImage() {
        return image;
    }

    public String getRecognitionResult() {
        return recognitionResult;
    }

    public String getSuggestion() {
        return suggestion;
    }

    public void setImage(Bitmap image) {
        this.image = image;
    }

    public void setRecognitionResult(String recognitionResult) {
        this.recognitionResult = recognitionResult;
    }

    public void setSuggestion(String suggestion) {
        this.suggestion = suggestion;
    }

    public boolean isEatable() {
        return eatable;
    }

    public void setEatable(boolean eatable) {
        this.eatable = eatable;
    }
    public String getImageURL() {
        return ImageURL;
    }

    public void setImageURL(String imageURL) {
        ImageURL = imageURL;
    }
    public static void updateImageDataByRecognitionResult(ArrayList<ImageData> imageDataList, String response, String scientificBasis, boolean eatable) {
        Iterator<ImageData> imageDataIterator = imageDataList.iterator();
        while (imageDataIterator.hasNext()) {
            ImageData id;
            if ((id = imageDataIterator.next()).getRecognitionResult().equals(response.substring(response.indexOf("食物：") + 3, response.indexOf("{")))) {
                id.setEatable(eatable);
                id.setSuggestion(scientificBasis);
                Log.d("当前ImageData：", id.toString());
            }
        }
    }
}
